import pathlib


class PathBuilder:
    def __init__(self, toolPath):
        self.currentPath = toolPath
        self.currentFile = pathlib.Path(__file__).resolve()

    # 获取最外层的根路径
    def getRootPath(self):
        pathLists = self.currentFile.parent.parts
        rootPath = self.currentFile.parents

        lastIndex = -1
        step = None
        # 获取当前文件在第几层
        for i in range(len(pathLists)):
            if pathLists[lastIndex - i] == self.currentPath:
                step = abs(lastIndex - i)
        return rootPath[step]

    # 设置需要去到的文件路径
    def joinFullPath(self, targetPath, targetFile=None):
        if targetFile is None:
            fullFilePath = pathlib.Path.joinpath(self.getRootPath(), targetPath)
        else:
            fullFilePath = pathlib.Path.joinpath(self.getRootPath(), targetPath, targetFile)

        return fullFilePath


